<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/25
 * Time: 15:04
 */

namespace App\Http\Controllers;


use App\Comment;
use App\Film;


class FilmController extends Controller
{
    function add(){
        return view('addFilm');
    }
    function save(){
        $data=Film::create([
            'title'=>$_POST['title'],'director'=>$_POST['director'],'writer'=>$_POST['writer'],
            'actor'=>$_POST['actor'],'type'=>$_POST['type'],'country'=>$_POST['country'],
            'language'=>$_POST['language'],'date'=>$_POST['date'],'time'=>$_POST['time'],
            'alias'=>$_POST['alias'],'poster'=>$_POST['poster'],'introduce'=>$_POST['introduce'],
            'screening'=>$_POST['screening']
        ]);
        if($data){
            return redirect(url('index'));
        }
        else{
            echo "新增失败";
        }
    }
    function update($id){
        $data=Film::where('id','=',$id)->update([
                'title'=>$_POST['title'],'director'=>$_POST['director'],'writer'=>$_POST['writer'],
                'actor'=>$_POST['actor'],'type'=>$_POST['type'],'country'=>$_POST['country'],
                'language'=>$_POST['language'],'date'=>$_POST['date'],'time'=>$_POST['time'],
                'alias'=>$_POST['alias'],'introduce'=>$_POST['introduce']
            ]);
        if($data){
            return redirect(url("detail/$id"));
        }
        else{
            echo "编辑失败";
        }
    }
    function detail($id){

        $detail=Film::find($id);
        $total=Comment::where('film_id','=',$id)->get();//所有评论
        $num=count($total);//评论数
        $comment=Comment::where('film_id','=',$id);
        if (isset($_GET['tag'])&&$_GET['tag']=='new'){
            $comment=$comment->orderby('time','desc')->paginate(5);
        }
        else{$comment=$comment->orderby('assist_num','desc')->paginate(5);}
        if($num>0){
            $detail->comment_num=$num;
            $sum=0;
            foreach ($total as $item){
                $sum+=$item['rating'];
            }
            $average=$sum/$num;
            $average=round($average,1);
            $detail->score=$average;
            $detail->save();
            return view('detail',['detail'=>$detail,'comment'=>$comment,'num'=>$num]);
        }
        else{
            return view('detail',['detail'=>$detail,'comment'=>$comment,'average'=>'暂无评分','num'=>0]);
        }
    }

    function search(){
        $title=$_POST['title'];
        $res=Film::where('title','=',$title)->get();
        $res=$res->toArray();
        if($res){
            return redirect(url("detail").'/'.$res[0]['id']);
        }
        else{
            return view('search');
        }
    }
    function rank(){
        $rank=Film::orderby('score','desc')->offset(0)->limit(20)->get();
        $rank=json_encode($rank);
        return $rank;
    }
    function offline(){
        $id=$_POST['id'];
        $film=Film::find($id);
        $film->screening=0;
        $res=$film->save();
        if($res){
            return 'success';
        }
        else{
            return 'fail';
        }
    }
    function delete(){
        $id=$_POST['id'];
        $film=Film::find($id);
        $res=$film->delete();
        if($res){
            return 'success';
        }
        else{
            return 'fail';
        }
    }
}